#include "Ball.h"
#include "EntityCategory.h"

const float radius = 3.f;

Ball::Ball()
{

}

Ball::Ball(WindowManager* win, b2World* wo, b2Vec2 pos, b2Vec2 dir)
{

	world = wo;
	window = win;
	dead = false;
	dying = false;

	b2Vec2 unit(dir.x-pos.x, dir.y-pos.y);

	unit = b2Vec2(unit.x/unit.Length(), unit.y/unit.Length());

	b2BodyDef bodyDef;
	bodyDef.position = b2Vec2(pos.x/win->getScale(), pos.y/win->getScale());
	bodyDef.type = b2_dynamicBody;
    body = world->CreateBody(&bodyDef);

	b2CircleShape shape;
	shape.m_p.Set(0, 0);
	shape.m_radius = radius/win->getScale();
    b2FixtureDef fixtureDef;
    fixtureDef.density = 2.f;
    fixtureDef.shape = &shape;
	fixtureDef.filter.categoryBits = BALL;
	fixtureDef.filter.maskBits = GROUND | CHARACTER;
    body->CreateFixture(&fixtureDef);

	body->SetUserData(this);

	body->ApplyLinearImpulse(b2Vec2(unit.x*2, unit.y*2), body->GetWorldCenter());
	
	
}


Ball::~Ball(void)
{
}

Ball* Ball::getThis()
{
	return this;
}

bool Ball::drawBall()
{
	if (!dead && !dying)
	{
		sf::CircleShape sprite(radius, 100);
		sprite.setFillColor(sf::Color::White);
		sprite.setOutlineThickness(1);
		sprite.setOutlineColor(sf::Color::Black);
		sprite.setOrigin(radius, radius);
		sprite.setPosition((window->getScale() * body->GetPosition().x), (window->getScale() * body->GetPosition().y));
		sprite.setRotation(body->GetAngle() * 180/b2_pi);
		window->draw(sprite);
	}
	else if (dying){
		body->GetWorld()->DestroyBody(body);
		dead = true;
		dying = false;
	}
	else if (dead)
	{
		return false;
	}
	return true;
}


void Ball::remove()
{
	dying = true;
}

sf::Vector2f Ball::getLocation()
{
	return sf::Vector2f((window->getScale() * body->GetPosition().x), (window->getScale() * body->GetPosition().y));
}